const app = getApp()
import http from '../../../utils/http'
Page({
    data: {
        foodInfo: null,
        roleId: -1,
        hideModal: true, //模态框的状态  true-隐藏  false-显示
    },
    onLoad: function (options) {
        console.log(options)
        this.setData({
            roleId: app.globalData.roleId
        })
        http({
            url: '/food/query/' + options.id,
            success: (result) => {
                console.log(result)
                this.setData({
                    foodInfo: result.info
                })
            }
        })
    },

    onShareAppMessage: function () {

    },

    toUpdateFood() {
        wx.navigateTo({
            url: '/pages/food_inner/update/update?id=' + this.data.foodInfo.id
        })
    },

    toDeleteFood() {
        var that = this
        wx.showModal({
            title: '提示',
            content: '确认删除吗？',
            success(res) {
                if (res.confirm) {
                    http({
                        url: '/food/delete/' + that.data.foodInfo.id,
                        success: (result) => {
                            console.log(result)
                            wx.showModal({
                                title: '提示',
                                content: '删除成功！',
                                success(res) {
                                    if (res.confirm) {
                                        wx.switchTab({
                                            url: '../../food/food'
                                        });
                                    } else if (res.cancel) {

                                    }
                                }
                            })
                        }
                    })
                } else if (res.cancel) {

                }
            }
        })
    },

    addOrder() {
        // wx.showModal({
        //     title: '提示',
        //     content: '本餐已卖完，下次记得早点哦~',
        // })
        var that = this
        http({
            url: '/order/today',
            success: (result) => {
                console.log(result)
                if (result.info != null) {
                    wx.showModal({
                        title: '提示',
                        content: '一餐只能下一单哦~',
                    })
                } else {
                    wx.login({
                        success: res => {
                            if (res.code) {
                                http({
                                    // url: '/wxPay/' + 1 + '/' + res.code,
                                    url: '/wxPay/' + that.data.foodInfo.sellingPrice + '/' + res.code,
                                    success: (result) => {
                                        var payResponse = result.payResponse;
                                        wx.requestPayment({
                                            timeStamp: payResponse.timeStamp,
                                            nonceStr: payResponse.nonceStr,
                                            package: payResponse.package,
                                            signType: payResponse.signType,
                                            paySign: payResponse.paySign,
                                            success() {
                                                // 微信支付成功,发起订单创建
                                                http({
                                                    url: '/order/create/' + that.data.foodInfo.id,
                                                    success: (result) => {
                                                        wx.showToast({
                                                            title: '下单成功',
                                                            icon: '',
                                                            duration: 1000,
                                                        })
                                                        wx.requestSubscribeMessage({
                                                            tmplIds: ['CfebW0YTdzFe7LPfV2MS39LHmVr0bvuRttgOzqhjHlE'],
                                                            success(res) {
                                                                wx.switchTab({
                                                                  url: '/pages/order/order',
                                                                })
                                                            },
                                                            fail(res) {
                                                                wx.switchTab({
                                                                    url: '/pages/order/order',
                                                                  })
                                                            }
                                                        })
                                                    },
                                                    fail: function (e) {
                                                        console.log(e)
                                                        wx.showModal({
                                                            title: '提示',
                                                            content: '下单失败，请联系客服',
                                                        })
                                                    }
                                                })
                                            },
                                            fail: function (e) {
                                                console.log(e)
                                                if (e.errMsg != "requestPayment:fail cancel") {
                                                    wx.showModal({
                                                        title: '提示',
                                                        content: '下单失败，请联系客服',
                                                    })
                                                }
                                            }
                                        })
                                    }
                                })
                            } else {
                                wx.hideLoading()
                                wx.showModal({
                                    title: '提示',
                                    content: '服务器连接失败，请检查网络',
                                })
                            }
                        },
                        fail: function () {
                            wx.hideLoading()
                            wx.showModal({
                                title: '提示',
                                content: '服务器连接失败，请检查网络',
                            })
                        }
                    })
                }
            }
        })

    },


    // 显示遮罩层 
  showModal: function() {
    var that = this;
    that.setData({
      hideModal: false
    })
    var animation = wx.createAnimation({
      duration: 600, //动画的持续时间 默认400ms 数值越大，动画越慢 数值越小，动画越快 
      timingFunction: 'ease', //动画的效果 默认值是linear 
    })
    this.animation = animation
    setTimeout(function() {
      that.fadeIn(); //调用显示动画 
    }, 200)
  },
 
  // 隐藏遮罩层 
  hideModal: function() {
    var that = this;
    var animation = wx.createAnimation({
      duration: 800, //动画的持续时间 默认400ms 数值越大，动画越慢 数值越小，动画越快 
      timingFunction: 'ease', //动画的效果 默认值是linear 
    })
    this.animation = animation
    that.fadeDown(); //调用隐藏动画 
    setTimeout(function() {
      that.setData({
        hideModal: true
      })
    }, 720) //先执行下滑动画，再隐藏模块 
  },
 
  //动画集 
  fadeIn: function() {
    this.animation.translateY(0).step()
    this.setData({
      animationData: this.animation.export() //动画实例的export方法导出动画数据传递给组件的animation属性 
    })
  },
  fadeDown: function() {
    this.animation.translateY(300).step()
    this.setData({
      animationData: this.animation.export(),
    })
  },
})